/**
 * Copyright 2017 Bartosz Schiller
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ashlikun.pdfview

import com.ashlikun.pdfview.util.PdfViewUtil
import kotlin.math.max
import kotlin.math.min

internal class PagesLoader(private val pdfView: PDFView) {
    private var xOffset = 0f
    private var yOffset = 0f

    /**
     * 计算需要显示的页数区间
     *
     * @return 前后包含
     */
    private fun getRenderPageRange(firstXOffset: Float, firstYOffset: Float, lastXOffset: Float, lastYOffset: Float): IntRange {
        val fixedFirstXOffset = -min(firstXOffset, 0f)
        val fixedFirstYOffset = -min(firstYOffset, 0f)
        val fixedLastXOffset = -min(lastXOffset, 0f)
        val fixedLastYOffset = -min(lastYOffset, 0f)
        val offsetFirst = if (pdfView.config.isVertical) fixedFirstYOffset else fixedFirstXOffset
        val offsetLast = if (pdfView.config.isVertical) fixedLastYOffset else fixedLastXOffset
        val allPageCount = pdfView.pdfFile!!.pagesCount
        var firstPage = pdfView.pdfFile!!.getPageAtOffset(offsetFirst, pdfView.zoom)
        var lastPage = pdfView.pdfFile!!.getPageAtOffset(offsetLast, pdfView.zoom)
        if (PdfViewUtil.DEBUG_MODE) PdfViewUtil.logi("getRenderPageRange   firstPage = ${firstPage}  ,,,  lastPage = $lastPage  fixedFirstXOffset=$fixedFirstXOffset,,,fixedFirstYOffset=$fixedFirstYOffset")
        //        int pageCount = lastPage - firstPage + 1;
        //前后多一页
        firstPage = max(0, firstPage - pdfView.config.pageLimit)
        lastPage = min(lastPage + pdfView.config.pageLimit, allPageCount - 1)
        return firstPage..lastPage
    }

    private fun loadVisible() {
        val firstXOffset = -xOffset
        val lastXOffset = -xOffset - pdfView.width
        val firstYOffset = -yOffset
        val lastYOffset = -yOffset - pdfView.height
        val rangeList = getRenderPageRange(firstXOffset, firstYOffset, lastXOffset, lastYOffset)
//        Util.loge("loadVisible  " + rangeList);
        pdfView.currentPageRange = rangeList
        rangeList.forEach {
            loadPage(it)
        }
    }

    private fun loadPage(page: Int) {
        val pageSize = if (pdfView.config.isNeedZoomBitmap) pdfView.pdfFile!!.getScaledPageSize(page, pdfView.zoom) else pdfView.pdfFile!!.getPageSize(page)
        //是否已经加载
        if (!pdfView.cacheManager.contains(page, pdfView.zoom)) {
            pdfView.renderingJob!!.addRenderingTask(page, pageSize.width, pageSize.height, pdfView.config.isBestQuality, pdfView.config.isAnnotationRendering, pdfView.zoom)
        }
    }

    fun loadPages() {
        xOffset = -min(pdfView.currentXOffset, 0f)
        yOffset = -min(pdfView.currentYOffset, 0f)
        loadVisible()
    }
}
